package view;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Locale;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import model.IExchangerModel;
import controller.IExchangerController;
import fileFiltering.ExchangerFilter;
import fileFiltering.Utils;

/**
 * Classe correspondant a la bar de menu du convertisseur
 * 
 * @author Ferreira & Richier
 * 
 */
public class ExchangerMenuBar extends JMenuBar implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IExchangerView view;
	private JMenuItem newMenuItem, modifyMenuItem, importMenuItem,
			exportToCSVMenuItem, exportToXMLMenuItem, exitMenuItem,
			copyMenuItem, pasteMenuItem, preferencesMenuItem,
			helpContentsMenuItem, aboutMenuItem;
	private JFileChooser fc;
	//private IExchangerController control;
	//private ResourceBundleWrapper resourceBundleWrapper;
	private JMenu currencies, edit, help;
	private PreferencesDialog preferencesDialog;
	//private IExchangerModel model;
	private CurrenciesTableDialog currenciesTableDialog;
	//private ClipBoardAccess clipBoardAccess;
	private AddCurrencyDialog addCurrencyDialog;

	/*public void setControl(IExchangerController control) {
		this.control = control;
	}//*/

	public ExchangerMenuBar(IExchangerModel model, IExchangerView view,
			ResourceBundleWrapper resourceBundleWrapper,
			ClipBoardAccess clipBoardAccess) {
		super();
		this.view = view;
		//this.model = 
		//this.resourceBundleWrapper = resourceBundleWrapper;
		//this.clipBoardAccess = clipBoardAccess;
		this.fc = new JFileChooser();
		this.fc.setFileFilter(new ExchangerFilter());
		this.fc.setAcceptAllFileFilterUsed(false);
		this.buildCurrenciesMenu();
		this.buildEditMenu();
		this.buildHelpMenu();
	}

	private void buildCurrenciesMenu() {
		ResourceBundleWrapper resourceBundleWrapper = ((ExchangerView)this.view).getResourceBundleWrapper();
		this.currencies = new JMenu(
				resourceBundleWrapper.getString("currencies"));
		this.newMenuItem = new JMenuItem(resourceBundleWrapper.getString("new"));
		this.newMenuItem.addActionListener(this);
		this.modifyMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("modify"));
		this.modifyMenuItem.addActionListener(this);
		this.importMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("import"));
		this.importMenuItem.addActionListener(this);
		this.exportToCSVMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("exportCSV"));
		this.exportToXMLMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("exportXML"));
		this.exportToXMLMenuItem.addActionListener(this);
		this.exportToCSVMenuItem.addActionListener(this);
		this.exitMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("exit"));
		this.exitMenuItem.addActionListener(this);
		currencies.add(this.newMenuItem);
		currencies.add(this.modifyMenuItem);
		currencies.add(this.importMenuItem);
		currencies.addSeparator();
		currencies.add(this.exportToCSVMenuItem);
		currencies.add(this.exportToXMLMenuItem);
		currencies.addSeparator();
		currencies.add(this.exitMenuItem);
		this.add(currencies);
	}

	private void buildEditMenu() {
		ResourceBundleWrapper resourceBundleWrapper = ((ExchangerView)this.view).getResourceBundleWrapper();
		this.edit = new JMenu(resourceBundleWrapper.getString("edit"));
		this.copyMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("copy"));
		this.copyMenuItem.addActionListener(this);
		this.pasteMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("paste"));
		this.pasteMenuItem.addActionListener(this);
		this.preferencesMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("preferences"));
		this.preferencesMenuItem.addActionListener(this);
		edit.add(this.copyMenuItem);
		edit.add(this.pasteMenuItem);
		edit.addSeparator();
		edit.add(this.preferencesMenuItem);
		this.add(edit);
	}

	private void buildHelpMenu() {
		ResourceBundleWrapper resourceBundleWrapper = ((ExchangerView)this.view).getResourceBundleWrapper();
		this.help = new JMenu(resourceBundleWrapper.getString("help"));
		this.helpContentsMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("helpContents"));
		this.aboutMenuItem = new JMenuItem(
				resourceBundleWrapper.getString("about"));
		this.aboutMenuItem.addActionListener(this);
		help.add(this.helpContentsMenuItem);
		help.add(aboutMenuItem);
		this.add(help);
	}

	private void updateTextAttribute() {
		ResourceBundleWrapper resourceBundleWrapper = ((ExchangerView)this.view).getResourceBundleWrapper();
		this.currencies.setText(resourceBundleWrapper.getString("currencies"));
		this.newMenuItem.setText(resourceBundleWrapper.getString("new"));
		this.modifyMenuItem.setText(resourceBundleWrapper.getString("modify"));
		this.importMenuItem.setText(resourceBundleWrapper.getString("import"));
		this.exportToCSVMenuItem.setText(resourceBundleWrapper
				.getString("exportCSV"));
		this.exportToXMLMenuItem.setText(resourceBundleWrapper
				.getString("exportXML"));
		this.exitMenuItem.setText(resourceBundleWrapper.getString("exit"));
		this.edit.setText(resourceBundleWrapper.getString("edit"));
		this.copyMenuItem.setText(resourceBundleWrapper.getString("copy"));
		this.pasteMenuItem.setText(resourceBundleWrapper.getString("paste"));
		this.preferencesMenuItem.setText(resourceBundleWrapper
				.getString("preferences"));
		this.help.setText(resourceBundleWrapper.getString("help"));
		this.helpContentsMenuItem.setText(resourceBundleWrapper
				.getString("helpContents"));
		this.aboutMenuItem.setText(resourceBundleWrapper.getString("about"));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		IExchangerController control = ((ExchangerView)this.view).getControl();
		IExchangerModel model = ((ExchangerView)this.view).getModel();
		ResourceBundleWrapper resourceBundleWrapper = ((ExchangerView)this.view).getResourceBundleWrapper();
		ClipBoardAccess clipBoardAccess = ((ExchangerView)this.view).getClipBoardAccess();
		if (e.getSource() == this.importMenuItem) {
			UIManager.getDefaults().setDefaultLocale(Locale.getDefault());
			JComponent.setDefaultLocale(Locale.getDefault());
			this.fc = new JFileChooser();
			this.fc.setFileFilter(new ExchangerFilter());
			this.fc.setCurrentDirectory(new File("."));
			this.fc.setAcceptAllFileFilterUsed(false);
			int returnVal = fc.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				control.loadFile(f);
			}
		} else if (e.getSource() == this.preferencesMenuItem) {
			if(this.preferencesDialog != null)
				this.preferencesDialog.dispose();
			this.preferencesDialog = new PreferencesDialog(control,
					resourceBundleWrapper);
		} else if (e.getSource() == this.modifyMenuItem) {
			if(this.currenciesTableDialog != null)
				this.currenciesTableDialog.dispose();
			this.currenciesTableDialog = new CurrenciesTableDialog(
					control, model, resourceBundleWrapper);
		} else if (e.getSource() == this.newMenuItem) {
			if(this.addCurrencyDialog != null)
				this.addCurrencyDialog.dispose();
			this.addCurrencyDialog = new AddCurrencyDialog(control, resourceBundleWrapper, null);
		} else if (e.getSource() == this.copyMenuItem) {
			clipBoardAccess.setClipboardContentsWithTextFieldContents();
		} else if (e.getSource() == this.pasteMenuItem) {
			clipBoardAccess.setTextFieldContentsWithClipboardContents();
		} else if (e.getSource() == this.exportToCSVMenuItem
				|| e.getSource() == this.exportToXMLMenuItem) {
			this.fc = new JFileChooser();
			this.fc.setFileFilter(new ExchangerFilter());
			this.fc.setCurrentDirectory(new File("."));
			this.fc.setAcceptAllFileFilterUsed(false);
			int returnVal = fc.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				if (e.getSource() == this.exportToXMLMenuItem)
					control.saveFile(f, Utils.xml);
				else
					control.saveFile(f, Utils.csv);
			}
		} else if(e.getSource() == this.aboutMenuItem){
			this.view.showMessageDialog("greeting");
		}
		else if (e.getSource() == this.exitMenuItem) {
			System.exit(1);
		}

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		updateTextAttribute();
		if (this.preferencesDialog != null) {
			this.preferencesDialog.repaint();
		}
		if (this.currenciesTableDialog != null) {
			this.currenciesTableDialog.repaint();
		}
		if(this.addCurrencyDialog != null){
			this.addCurrencyDialog.repaint();
		}
	}
}
